#============================================================
#	Xian Jing
#============================================================
# @datetime: 2022-3-18 14:34:04
#============================================================
extends Path2D


export var damage : float = 0
export var speed : float = 10 setget set_speed
export var rollback : bool = true


onready var _area : Area2D = $PathFollow2D/Area2D
onready var _follow : PathFollow2D = $PathFollow2D

var _t : float = 0.0
var _time : float = 0.0
var _direction : int = 1


func set_speed(value: float) -> void:
	if value > 0:
		speed = value
	else:
		printerr("speed 必须大于 0")


#============================================================
#   内置
#============================================================
func _ready():
	assert(_area.connect("body_entered", self, "body_entered") == OK)
	var length = curve.get_baked_length()
	_time = length / speed


func _physics_process(delta):
	_t += delta * _direction
	if _t >= _time or _t <= 0:
		if rollback:
			_direction *= -1
		else:
			_t = 0
	_follow.unit_offset = _t / _time



#============================================================
#   连接信号
#============================================================
func body_entered(body: Node):
	if body.has_method("take_damage"):
		body.take_damage(damage)


